package com.goods.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.goods.mapper.OrderMapper;
import com.goods.pojo.Book;
import com.goods.pojo.Order;
import com.goods.pojo.OrderItem;
import com.goods.pojo.Page;
import com.goods.pojo.PageConstants;
import com.goods.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService{

	@Resource
	private OrderMapper orderMapper;

	// 我的订单
	@Override
	public Page<Order> myOrders(String uid, int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		//获取中记录数
		int tr = orderMapper.Count(uid,0);
		//当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		
		List<Order> orderList = orderMapper.find(uid,0, pc1, ps);
		for(Order o : orderList) {
			o.setOwner(orderMapper.findUserByUid(o.getUid()));
			o.setOrderItemList(orderMapper.loadOrderItem(o.getOid()));
			for(OrderItem oi : o.getOrderItemList()) {
				oi.setBook(orderMapper.findByBid(oi.getBid()));
				oi.setOrder(o);
			}
		}
		Page<Order> pb = new Page<Order>();
		pb.setBeanList(orderList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}

	// 修改订单状态
	@Override
	public int updateStatus(String oid, int status) {
		
		return orderMapper.updateStatus(oid, status);
	}

	// 查询订单状态
	@Override
	public int findStatus(String oid) {
		// TODO Auto-generated method stub
		return orderMapper.findStatus(oid);
	}
	// 加载订单
	@Override
	public Order load(String oid) {
		Order o = orderMapper.load(oid);
		o.setOwner(orderMapper.findUserByUid(o.getUid()));
		o.setOrderItemList(orderMapper.loadOrderItem(oid));
		for(OrderItem oi : o.getOrderItemList()) {
			oi.setBook(orderMapper.findByBid(oi.getBid()));
			oi.setOrder(o);
		}
		return o;
	}

	// 生成订单
	@Override
	public int createOrder(Order order) {
		orderMapper.add(order);
		int index = 0;
		for(OrderItem oi : order.getOrderItemList()) {
			int i = orderMapper.addOrderItem(oi);
			if(i!=0) {
				index++;
			}
		}
		return index;
	}
	
	// 后台查询所有订单
	@Override
	public Page<Order> findAll(int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		//获取中记录数
		int tr = orderMapper.Count(null,0);
		//当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		
		List<Order> orderList = orderMapper.find(null,0, pc1, ps);
		for(Order o : orderList) {
			o.setOwner(orderMapper.findUserByUid(o.getUid()));
			o.setOrderItemList(orderMapper.loadOrderItem(o.getOid()));
			for(OrderItem oi : o.getOrderItemList()) {
				oi.setBook(orderMapper.findByBid(oi.getBid()));
				oi.setOrder(o);
			}
		}
		Page<Order> pb = new Page<Order>();
		pb.setBeanList(orderList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}
	
	// 后台按状态查询订单
	@Override
	public Page<Order> findByStatus(int status, int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		//获取中记录数
		int tr = orderMapper.Count(null,status);
		//当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		
		List<Order> orderList = orderMapper.find(null,status, pc1, ps);
		for(Order o : orderList) {
			o.setOwner(orderMapper.findUserByUid(o.getUid()));
			o.setOrderItemList(orderMapper.loadOrderItem(o.getOid()));
			for(OrderItem oi : o.getOrderItemList()) {
				oi.setBook(orderMapper.findByBid(oi.getBid()));
				oi.setOrder(o);
			}
		}
		Page<Order> pb = new Page<Order>();
		pb.setBeanList(orderList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}
}
